/*
 * timelist.c
 *  该模块用于对任务表信息做数据链表管理
 *  Created on: 2025.5.27
 *      Author: TracyHenry
 */
#include "timelist.h"
#include "taskStorage.h"
#include "w25q16.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

TimeTableNode *timeTable = NULL;
uint8_t timeTableNum = 0;
 /******************************************************************************
 * FunctionName : createNode
 * Description  : 创建新节点
 * Parameters   : uint8_t id, const char *eventName, uint8_t hour, uint8_t minute
 * Returns      : TimeTableNode
*******************************************************************************/
TimeTableNode* createNode(uint8_t year, uint8_t month, uint8_t date, uint8_t hour, uint8_t minute, uint16_t duration, uint8_t speedA, uint8_t speedB) 
{
    TimeTableNode *newNode = (TimeTableNode*)malloc(sizeof(TimeTableNode));
    if (newNode == NULL) 
		{
        printf("ram malloc fail\n");
        return NULL;
    }
    
    newNode->year = year;
    newNode->month = month;
    newNode->date = date;
    newNode->hour = hour;
    newNode->minute = minute;
    newNode->duration = duration;
    newNode->speedA = speedA;
    newNode->speedB = speedB;
    newNode->next = NULL;
    
    return newNode;
}

 /******************************************************************************
 * FunctionName : 插入节点到链表（按时间顺序）
 * Description  : 插入节点到链表（按时间顺序）
 * Parameters   : TimeTableNode **head, TimeTableNode *newNode
 * Returns      : void
*******************************************************************************/ 
// 
void insertNode(TimeTableNode **head, TimeTableNode *newNode) 
{
    if (*head == NULL 
        || (*head)->year > newNode->year 
        || ((*head)->year == newNode->year && (*head)->month > newNode->month)
        || ((*head)->year == newNode->year && (*head)->month == newNode->month && (*head)->date > newNode->date)
        || ((*head)->year == newNode->year && (*head)->month == newNode->month && (*head)->date == newNode->date  && (*head)->hour > newNode->hour)
        || ((*head)->year == newNode->year && (*head)->month == newNode->month && (*head)->date == newNode->date  && (*head)->hour == newNode->hour && (*head)->minute > newNode->minute)) 
	  {
        newNode->next = *head;
        *head = newNode;
    } 
    else 
	  {
        TimeTableNode *current = *head;
        while (current->next != NULL && 
               !(current->next->year > newNode->year 
                || (current->next->year == newNode->year && current->next->month > newNode->month)
                || (current->next->year == newNode->year && current->next->month == newNode->month && current->next->date > newNode->date)
                || (current->next->year == newNode->year && current->next->month == newNode->month && current->next->date == newNode->date && current->next->hour > newNode->hour)
                || (current->next->year == newNode->year && current->next->month == newNode->month && current->next->date == newNode->date && current->next->hour == newNode->hour && current->next->minute > newNode->minute))) 
		    {
            current = current->next;
        }
        newNode->next = current->next;
        current->next = newNode;
    }
    timeTableNum ++;
}
 
 /******************************************************************************
 * FunctionName : findNode
 * Description  : 查找节点
 * Parameters   : TimeTableNode *head, uint8_t year, uint8_t month, uint8_t date, uint8_t hour, uint8_t minute
 * Returns      : TimeTableNode*
*******************************************************************************/ 
TimeTableNode* findNode(TimeTableNode *head, uint8_t year, uint8_t month, uint8_t date, uint8_t hour, uint8_t minute) 
{
    TimeTableNode *current = head;
    while (current != NULL) 
		{
        if (current->year == year 
            && current->month == month
            && current->date == date
            && current->hour == hour
            && current->minute == minute ) 
				{
            return current;
        }
        current = current->next;
    }
    return NULL;
}

 /******************************************************************************
 * FunctionName : traverseListByIndex
 * Description  : 通过索引值遍历
 * Parameters   : TimeTableNode* head
 * Returns      : void
*******************************************************************************/ 
uint8_t timeTableIndex = 0;
void traverseListByIndex(TimeTableNode* head) {
    TimeTableNode* current = head;
    uint8_t buffer[16];
    int index = 0;
    
    assert_param(timeTableNum > 0);

    while (current != NULL && index <= timeTableIndex) {

        if(index == timeTableIndex)
        {
            // printf("Index %d: %04d-%02d-%02d %02d:%02d, Duration: %d mins\n",
            //     index,
            //     current->year + 2000, // 假设年份从0开始，实际可能需要调整
            //     current->month,
            //     current->date,
            //     current->hour,
            //     current->minute,
            //     current->duration);
            buffer[0] = current->year;
            buffer[1] = current->month;
            buffer[2] = current->date;
            buffer[3] = current->hour;
            buffer[4] = current->minute;
            buffer[5] = (uint8_t)(current->duration >> 8);
            buffer[6] = (uint8_t)(current->duration);
            buffer[7] = current->speedA;
            buffer[8] = current->speedB;

            W25Q16_PageWrite(buffer,Storage_TimeList_ADDRESS_Sector + timeTableIndex * 16, 16);
        }

        index++;
        current = current->next;
    }
    timeTableIndex ++;
}
 
 /******************************************************************************
 * FunctionName : deleteNode
 * Description  : 删除节点
 * Parameters   : TimeTableNode **head, uint8_t year, uint8_t month, uint8_t date, uint8_t hour, uint8_t minute
 * Returns      : portBASE_TYPE
*******************************************************************************/ 
portBASE_TYPE deleteNode(TimeTableNode **head, uint8_t year, uint8_t month, uint8_t date, uint8_t hour, uint8_t minute) {
    if (*head == NULL) 
		{
        return pdFALSE;
    }
    
    TimeTableNode *current = *head;
    TimeTableNode *previous = NULL;
    
    while (current != NULL && (current->year != year || current->month != month || current->date != date || current->hour != hour || current->minute != minute)) 
		{
        previous = current;
        current = current->next;
    }
    
    if (current == NULL) 
	{
        return pdFALSE; // 没找到
    }
    
    if (previous == NULL) 
	{
        *head = current->next;
        timeTableNum --;
    } 
	else 
	{
        previous->next = current->next;
        timeTableNum --;
    }
    
    free(current);
    return pdPASS;
}
 
 /******************************************************************************
 * FunctionName : printfTimeTable
 * Description  : 打印所有节点
 * Parameters   : TimeTableNode *head
 * Returns      : void
*******************************************************************************/ 
void printfTimeTable(TimeTableNode *head) 
{
    TimeTableNode *current = head;
    printf("--------------------------------\n");
    while (current != NULL) 
	{
        printf("%d-%d-%d\t%02d:%02d %d\n", 
               current->year, current->month, current->date, current->hour, current->minute, current->duration);
        current = current->next;
    }
    printf("\n");
}
 
 /******************************************************************************
 * FunctionName : freeTimeTable
 * Description  : 释放链表内存
 * Parameters   : TimeTableNode *head
 * Returns      : void
*******************************************************************************/ 
void freeTimeTable(TimeTableNode *head) 
{
    TimeTableNode *current = head;
    while (current != NULL) 
	  {
        TimeTableNode *temp = current;
        current = current->next;
        free(temp);
    }
	  timeTable  = NULL;
      timeTableNum  = 0;
}


uint8_t getTableNum(void)
{
    return timeTableNum;
}
